/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-4
 * @Time  : 下午11:41
 * @brief :
 */
(function(){
    var Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
        $page = $('.page-security'),
        $summary = $page.find('.summary'),
        $idCard = $page.find('.id-card'),
        $setLogin = $page.find('.set-login'),
        $setTrade = $page.find('.set-trade'),
        dialog = new Dialog();



    P.createPage({
        _data:{},

        _create:function(){
            $summary.find('.btn-set').click(function(){
                var $ext = $(this).parents('.line').find('.ext'),
                    $line = $(this).parents('.line');
                if($ext.is(':visible')){
                    $(this).removeClass('btn-set-active');
                    $ext.slideUp();
                }else{
                    $(this).addClass('btn-set-active');
                    $ext.slideDown();
                }
            });
        },


        _init:function(){
            var me = this;
            $idCard.find('.btn-confirm').click(function(){
                if(!$(this).hasClass('btn-disable')){
                    me.idCard();
                }
            });

            $setLogin.find('.btn-confirm').click(function(){
                me.login();
            });

            $setTrade.find('.btn-confirm').click(function(){
                me.trade();
            });
        },

        //实名认证
        idCard:function(){
            var $tip = $idCard.find('.error-tip'),
                id_card      = $idCard.find('.card-code').val(),
                re_id_card   = $idCard.find('.re-card-code').val(),
                id_card_name = $idCard.find('.card-name').val();
            $tip.hide();

            if(!id_card_name) {
                $tip.show().text('请输入姓名');
                return;
            }
            if(!id_card) {
                $tip.show().text('请输入身份证号码');
                return;
            }
            if(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(id_card) == false) {
                $tip.show().text('请输入正确的身份证号');
                return;
            }

            if(re_id_card!=id_card){
                $tip.show().text('两次身份证号号码输入不一致');
                return;
            }

            $idCard.find('.btn-confirm').addClass('btn-disable');
            $.post("/account/setidcard",{
                id_card_name:id_card_name,
                id_card:id_card
            },function(ret,status){
                if(ret.error_no == 0){
                    dialog.show('实名认证成功');
                    $page.find('.id-line .btn-set').trigger("click");
                    location.reload();
                }else{
                    if(ret.error_no == 10009){
                        dialog.show('' +
                            '<div style="text-align: left;line-height: 25px;">因为公安部身份系统在升级，您需要手动认证实名信息，请提交您的身份证正反面及手持身份证照片（' +
                            '<a href="/user/manidverify" class="text-link">查看事例</a>' +
                            '）到<a href="mailto:service@zhenrongbao.com" class="text-link">service@zhenrongbao.com</a>邮箱，我们会在1个工作日内帮您审核通过。</div>' +
                            '');
                        $idCard.find('.btn-confirm').removeClass('btn-disable');
                        return;
                    }
                    $tip.show().text(ret.error_message);
                    $idCard.find('.btn-confirm').removeClass('btn-disable');
                }
            },"json");
        },

        //登录密码
        login:function(){
            var $tip = $setLogin.find('.error-tip'),
                code = $setLogin.find('.iphone-code').val(),
                password = $setLogin.find('.password').val(),
                rePassword = $setLogin.find('.re-password').val();
            $tip.hide();

            if(!/^\d{6}$/.test(code)){
                $tip.show().text('验证码必须为6位的整数');
                return;
            }

            if(password.length<6 || password.length>16){
                $tip.show().text('密码的长度必须为6-16位');
                return;
            }

            if(password!=rePassword){
                $tip.show().text('两次输入的密码不一致');
                return;
            }

            $.post('/account/modifypasswd',{
                'code':code,
                'passwd':password
            },function(ret,status){
                if(ret.error_no == 0){
                    dialog.show('密码修改成功');
                    $page.find('.login-line .btn-set').trigger("click").text('修改');
                    location.reload();
                }else{
                    $tip.show().text(ret.error_message);
                }
            },'json');
        },

        //交易密码
        trade:function(){
            var $tip = $setTrade.find('.error-tip'),
                code = $setTrade.find('.iphone-code').val(),
                password = $setTrade.find('.password').val(),
                rePassword = $setTrade.find('.re-password').val();

            $tip.hide();

            if(!/^\d{6}$/.test(code)){
                $tip.show().text('验证码必须为6位的整数');
                return;
            }

            if(password.length<6 || password.length>16){
                $tip.show().text('密码的长度必须为6-16位');
                return;
            }

            if(password!=rePassword){
                $tip.show().text('两次输入的密码不一致');
                return;
            }

            $.post('/account/modifytradepasswd',{
                'code':code,
                'trade_passwd':password
            },function(ret,status){
                if(ret.error_no == 0){
                    dialog.show('密码修改成功');
                    $page.find('.trade-line .btn-set').trigger("click").text('修改');
                    $page.find('.trade-line .center').text('已设置');
                    location.reload();
                }else{
                    $tip.show().text(ret.error_message);
                }
            },'json');
        }

    });
})();

